package com.lianzhihui.romeex.ui.wallet;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.BaiZhanTingActivityAdapter;
import com.lianzhihui.romeex.bean.FirstMinnerFindHomeInfoBean;
import com.lianzhihui.romeex.bean.FirstMinnerFindRecordInfoBean;
import com.lianzhihui.romeex.mvp.presenter.BaiZhanTingPresnterImp;
import com.lianzhihui.romeex.mvp.view.BaiZhanTingView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.widget.ZFEmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.text.MessageFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 * Author: RAMON
 * CreateDate: 11/15/20 5:18 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class BaizhantingActivity extends BaseActivity implements BaiZhanTingView, OnRefreshListener {
    @BindView(R.id.tv_op2)
    TextView tvOp2;
    @BindView(R.id.cb_op2)
    ImageView cbOp2;

    @BindView(R.id.tv_net)
    TextView tv_net;
    @BindView(R.id.bt_submit)
    TextView bt_submit;
    @BindView(R.id.tv_icon_price)
    TextView tv_icon_price;
    @BindView(R.id.tv_zhanzanting)
    TextView tv_zhanzanting;
    @BindView(R.id.tv_op1)
    TextView tvOp1;
    @BindView(R.id.cb_op1)
    ImageView cbOp1;
    @BindView(R.id.tv_pc)
    TextView tvPc;
    @BindView(R.id.tv_attend)
    TextView tvAttend;
    @BindView(R.id.tv_paidan)
    TextView tv_paidan;
    @BindView(R.id.tv_yaoqing)
    TextView tv_yaoqing;
    @BindView(R.id.mySmartRefreshLayout)
    SmartRefreshLayout mySmartRefreshLayout;
    @BindView(R.id.recyclerViewSmartRefreshLayout)
    SmartRefreshLayout recyclerViewSmartRefreshLayout;
    private final int pageSize = 20;

    @BindView(R.id.recyclerView)
    ZFEmptyRecyclerView recyclerView;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    private BaiZhanTingPresnterImp baiZhanTingPresnterImp;
    private BaiZhanTingActivityAdapter baiZhanTingActivityAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_baizhanting_layout;
    }

    @Override
    protected void initView() {
        includeTopTvTitle.setText(getResources().getString(R.string.baizhanting));
        mySmartRefreshLayout.setOnRefreshListener(this);
        baiZhanTingPresnterImp = new BaiZhanTingPresnterImp(this, this);
        mySmartRefreshLayout.autoRefresh();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baiZhanTingActivityAdapter = new BaiZhanTingActivityAdapter(this, new ArrayList<>());
        recyclerView.setAdapter(baiZhanTingActivityAdapter);
        recyclerViewSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                baiZhanTingPresnterImp.requestFirstMinnerFindrecord(pageNum, pageSize);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.bt_submit})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.bt_submit:
                showLoading("");
                baiZhanTingPresnterImp.requestFirstMinnerOpenFirstStatus();
                break;
            default:
                break;
        }
    }

    @Override
    public void success() {
        dismissLoading();
        bt_submit.setEnabled(false);
        bt_submit.setBackground(getResources().getDrawable(R.drawable.bt_gray_24dp));
        bt_submit.setText(getResources().getString(R.string.text_in_join));
        mySmartRefreshLayout.autoRefresh();
    }

    /**
     * 头矿首页响应
     *
     * @param bean
     */
    @Override
    public void firstMinnerFindhomeSuccess(FirstMinnerFindHomeInfoBean bean) {
        tv_paidan.setText(MessageFormat.format("{0} R", bean.getFirstHashrate()));
        tv_yaoqing.setText(MessageFormat.format("{0} R", bean.getInviteHashrate()));
        tvPc.setText(MessageFormat.format("{0} R", bean.getTotalHashrate()));
        tv_net.setText(MessageFormat.format("{0} R", bean.getLastTotalHashrate()));
        tvOp1.setText(MessageFormat.format("{0}", bean.getBalanceUSDT()));
        tvOp2.setText(MessageFormat.format("{0}", bean.getBalanceMain()));
        tv_icon_price.setText(MessageFormat.format("{0}{1}{2}", getResources().getString(R.string.text_finish_hint), bean.getCoinPrice(), getResources().getString(R.string.text_rex)));
        tv_zhanzanting.setText(MessageFormat.format("{0}{1}{2}{3}{4}", getResources().getString(R.string.baizhanting_text1), bean.getMinBalanceThreshold() + getResources().getString(R.string.text_usdt), getResources().getString(R.string.baizhanting_text2), bean.getMinBalanceThreshold() + getResources().getString(R.string.text_usdt), getResources().getString(R.string.baizhanting_text3)));
        if (bean.getCheckCoinName().equals(getResources().getString(R.string.text_rex))) {
            cbOp2.setVisibility(View.VISIBLE);
        } else if (bean.getCheckCoinName().equals(getResources().getString(R.string.text_usdt))) {
            cbOp1.setVisibility(View.VISIBLE);
        }
        if (bean.isOpenFirst()) {
            //开启
            bt_submit.setEnabled(false);
            if (bean.getCheckCoinName().isEmpty()) {
                //余额不足
                tvAttend.setVisibility(View.VISIBLE);
                bt_submit.setBackground(getResources().getDrawable(R.drawable.bt_gray_24dp));
                bt_submit.setText(getResources().getString(R.string.text_not_enough));
            } else {
                //余额充足
                bt_submit.setBackground(getResources().getDrawable(R.drawable.bt_gray_24dp));
                bt_submit.setText(getResources().getString(R.string.text_in_join));
            }
        } else {
            //未开启
            if (bean.getCheckCoinName().isEmpty()) {
                //余额不足
                bt_submit.setBackground(getResources().getDrawable(R.drawable.bt_gray_24dp));
                bt_submit.setText(getResources().getString(R.string.text_not_enough));
                bt_submit.setEnabled(false);
            } else {
                //余额充足
                bt_submit.setBackground(getResources().getDrawable(R.drawable.bt_theme_24dp));
                bt_submit.setText(getResources().getString(R.string.text_join_game));
            }
        }
        mySmartRefreshLayout.finishRefresh();
    }

    /**
     * 头矿挖矿记录响应
     *
     * @param bean
     */
    @Override
    public void firstMinnerFindrecordSuccess(FirstMinnerFindRecordInfoBean bean) {
        if (bean != null) {
            if (pageNum == 1) {
                recyclerView.showEmptyView(bean.getContent().size(), 1);
                baiZhanTingActivityAdapter.setNewData(bean.getContent());
            } else {
                baiZhanTingActivityAdapter.addData(bean.getContent());
            }
            pageNum++;
            recyclerViewSmartRefreshLayout.finishLoadMore();
        }
    }


    @Override
    public void setFailure() {
        dismissLoading();
        if (null != mySmartRefreshLayout) {
            mySmartRefreshLayout.finishRefresh();
        }
        if (null != recyclerViewSmartRefreshLayout) {
            recyclerViewSmartRefreshLayout.finishLoadMore();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        baiZhanTingPresnterImp.requestFirstMinnerFindhome();
        pageNum = 1;
        baiZhanTingPresnterImp.requestFirstMinnerFindrecord(pageNum, pageSize);
    }

}
